package com.sxfq.loansupermarket.backsystem.modular.consultInfo.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.sxfq.loansupermarket.backsystem.core.log.LogManager;
import com.sxfq.loansupermarket.backsystem.core.log.factory.LogTaskFactory;
import com.sxfq.loansupermarket.backsystem.core.shiro.ShiroKit;
import com.sxfq.loansupermarket.backsystem.core.support.HttpKit;
import com.sxfq.loansupermarket.backsystem.core.util.StringUtil;
import com.sxfq.loansupermarket.backsystem.modular.arrival.entity.CustomerInfo;
import com.sxfq.loansupermarket.backsystem.modular.arrival.entity.SearchCustomerInfo;
import com.sxfq.loansupermarket.backsystem.modular.consultInfo.dao.ConsultInfoDao;
import com.sxfq.loansupermarket.backsystem.modular.consultInfo.entity.ConsultInfo;
import com.sxfq.loansupermarket.backsystem.modular.consultInfo.entity.SearchConsultInfo;
import com.sxfq.loansupermarket.backsystem.modular.consultInfo.service.ConsultInfoService;
import com.sxfq.loansupermarket.backsystem.modular.tcCenter.entity.SearchConsultantInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
@Transactional
public class ConsultInfoServiceImpl implements ConsultInfoService {

    @Autowired
    private  ConsultInfoDao dao;

    @Override
    public List<ConsultInfo> selectPageListByQueryGetMap(Page<ConsultInfo> page,SearchConsultInfo searchInfo) {
        String tableHead = ShiroKit.getUser().getAbbreviation();
        String table1 =  tableHead+"consult_info";
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("page", page);
        if (StringUtil.isNotEmpty(searchInfo.getCaseNo())) {
            params.put("caseNo", searchInfo.getCaseNo());
        }
        if (StringUtil.isNotEmpty(searchInfo.getPhone())) {
            params.put("phone", searchInfo.getPhone());
        }
        if (StringUtil.isNotEmpty(searchInfo.getPatientName())) {
            params.put("patientName", searchInfo.getPatientName());
        }
        if (StringUtil.isNotEmpty(searchInfo.getReach())) {
            params.put("reach", searchInfo.getReach());
        }
        if (StringUtil.isNotEmpty(searchInfo.getStartCreateTime())) {
            params.put("startCreateTime", searchInfo.getStartCreateTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getEndCreateTime())) {
            params.put("endCreateTime", searchInfo.getEndCreateTime());
        }
        params.put("tableName", this.getTableName());
        params.put("table1", table1);
        params.put("startIndex", searchInfo.getStartIndex());
        params.put("limitIndex", searchInfo.getLimitIndex());
        return dao.getQueryPage(page, params);
    }


    public int getQueryPagesCount(Page<ConsultInfo> page, SearchConsultInfo searchInfo) {
        String tableHead = ShiroKit.getUser().getAbbreviation();
        String table1 =  tableHead+"consult_info";
        Map<String, Object> params = new HashMap<String, Object>();
        if (StringUtil.isNotEmpty(searchInfo.getCaseNo())) {
            params.put("caseNo", searchInfo.getCaseNo());
        }
        if (StringUtil.isNotEmpty(searchInfo.getPhone())) {
            params.put("phone", searchInfo.getPhone());
        }
        if (StringUtil.isNotEmpty(searchInfo.getPatientName())) {
            params.put("patientName", searchInfo.getPatientName());
        }
        if (StringUtil.isNotEmpty(searchInfo.getStartCreateTime())) {
            params.put("startCreateTime", searchInfo.getStartCreateTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getEndCreateTime())) {
            params.put("endCreateTime", searchInfo.getEndCreateTime());
        }
        if (StringUtil.isNotEmpty(searchInfo.getReach())) {
            params.put("reach", searchInfo.getReach());
        }
        params.put("page", page);
        params.put("tableName", this.getTableName());
        params.put("table1", table1);
        return dao.getQueryPagesCount(page, params);
    }
    //获取表名
    public String getTableName() {
        String tableHead = ShiroKit.getUser().getAbbreviation();
        String tableName =  tableHead+"consult_info";
        if (tableHead == "" || tableHead == null) {
            LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), HttpKit.getIp()));
            ShiroKit.getSubject().logout();
            return "/login.html";
        }
        return tableName;
    }

}


